package com.bkitmobile.kesu.appengine.mobile.server;

import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.*;
import org.json.JSONException;
import org.json.JSONObject;

import com.bkitmobile.kesu.appengine.storingdata.server.DataStoreManager;


@SuppressWarnings("serial")
public class MobileServiceServlet extends HttpServlet 
	{
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
	}
	public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
			HttpSession session = req.getSession(true);
			Object obj = session.getAttribute("username");
			String username = null;
			username = (String) obj;
			res.setContentType("text");
			String id = null;
			id = (String) req.getParameter("ID");
		
			if (id==null) {
				res.getWriter().println("is null");
				return;
			}
			String result = null;
			if (id.equals("Login"))
			{
				result = Responses.loginResp(req);
				if (result.startsWith("<save>"))
				{
					session.setAttribute("username", req.getParameter("username"));
					session.setAttribute("save", result.substring(6));
					boolean isInvisible = false;
					String invi = req.getParameter("invisible");
					if (invi.equals("invisible")) isInvisible = true;
					session.setAttribute("isAvailable",!isInvisible);
					JSONObject object = new JSONObject();
					try {
						object.put("result", "logged");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					result = object.toString();
				}
				else{
					session.setAttribute("username", req.getParameter("username"));
				}
				
			} else if (id.equals("ConfirmLogin"))
			{
				result = (String) session.getAttribute("save");
				DataStoreManager db = new DataStoreManager();
				boolean available = (Boolean) session.getAttribute("isAvailable");
				db.setLoggin(username, true, available);
			} else if (id.equals("Get24hList"))
			{//lay nhung nguoi xem dc 24h cua minh
				result = Responses.get24hListResp(username);
			} else if (id.equals("Get24hPosition"))
			{
				result = Responses.get24hPosition(req.getParameter("person"),username);
			} else if (id.equals("GetViewInviList"))
			{//lay nhung nguoi co the thay minh onl
				result = Responses.getInviList(username);
			} else if (id.equals("GetMyProfile"))
			{
				result = Responses.getMyProfile(username);
			} else if (id.equals("GetFriendProfile"))
			{
				result = Responses.getFriendProfile(username, req.getParameter("friend"));
			} else if (id.equals("UpdateList"))
            {
                result = Responses.UpdateFriendsList(username);
            } else if (id.equals("GetLocations"))
            {
            	result = Responses.locationsResp(username
            			,Double.parseDouble(req.getParameter("lat"))
            			,Double.parseDouble(req.getParameter("lng")));
            } else if (id.equals("GetPlaceInfo"))
            {
            	result = Responses.getPlaceInfoResp(username,req.getParameter("lat")
            			,req.getParameter("lng"));
            }else if (id.equals("Register"))
            {
                result = Responses.regResp(req.getParameter("username"),
                							req.getParameter("password"),
                							req.getParameter("firstname"),
                							req.getParameter("lastname"),
                							req.getParameter("birthday"),
                							req.getParameter("email"),
                							req.getParameter("sex").startsWith("f"),
                							true
                					);
            }
			else if (id.equals("GetFilterPlace")){
				result = Responses.getFilterPlace(username, 
						Double.parseDouble(req.getParameter("lat")), 
						Double.parseDouble(req.getParameter("lng")), 
						req.getParameter("filter"));
			} else if (id.equals("Search"))
			{
				result = Responses.search(req.getParameter("name")
						, req.getParameter("type")
						, Double.parseDouble(req.getParameter("distance"))
						, Double.parseDouble(req.getParameter("lat"))
						, Double.parseDouble(req.getParameter("lng"))
						);
			} else if (id.equals("GetAvatar")){
				result = Responses.getFrAvatar(username, req.getParameter("friend")); 
			}
			else result = id;
			result = URLEncoder.encode(result,"UTF-8");
			res.getWriter().println(result);
	}
	
}
